<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>首页</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            body {
                width: 80%;
                margin: 0 auto;
            }
            h1 {
                border-bottom: 1px darkgray solid;
            }
            a {
                text-decoration: none;
            }
            .del {
                margin-left: 100px;
                color: darkgreen;
            }
            .export {
                position: absolute;
                top: 10px;
                right: 150px;
                width: 90px;
                height: 30px;
                text-align: center;
                line-height: 30px;
                font-size: 12px;
                color: white;
                background-color: darkgreen;
            }
            .name {
                font: bold 20px Arial;
            }
            .favs {
                position: relative;
                top: -8px;
                left: 12px;
            }
            .fruits > li {
                border-bottom: 1px lightgray dashed;
                margin: 10px 0 20px 0;
                list-style: none;
            }
            .fruits > li > img {
                vertical-align: middle;
            }
            #chart {
                width: 600px;
                height: 400px;
                border: 1px solid black;
                position: fixed !important;
                top: 120px;
                right: 20px;
                background-color: lightgray;
            }
        </style>
    </head>
    <body>
        <h1>水果清单</h1>
        <a class="export" href="/export">导出数据</a>
        <div id="app">
            <ul class="fruits">
                <li v-for="fruit in fruits">
                    <img :src="'/static/images/' + fruit.image" width="32">
                    <span class="name">{{ fruit.name }}</span>
                    <a class="del" href="" @click.prevent="removeItem(fruit)">删除</a>
                    <p>{{ fruit.intro }}</p>
                    <a class="like" href="" @click.prevent="likeItem(fruit)">
                        <img src="/static/images/heart.png" width="32">
                    </a>
                    <span class="favs">{{ fruit.favs }}</span>
                </li>
            </ul>
        </div>
        <!-- 第1步：准备好绘图使用的div并通过CSS进行定制 -->
        <div id="chart"></div>
        <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js"></script>
        <script>
            let app = new Vue({
                el: '#app',
                data: {
                    fruits: []
                },
                created() {
                    fetch('/api/fruits').then(resp => resp.json()).then(json => {
                        this.fruits = json.fruits
                    })
                },
                methods: {
                    removeItem(fruit) {
                        fetch('/api/delete?id=' + fruit.id).then(resp => resp.json()).then(json => {
                            if (json.code === 10000) {
                                let pos = this.fruits.indexOf(fruit)
                                this.fruits.splice(pos, 1)
                            }
                        })

                    },
                    likeItem(fruit) {
                        fetch('/api/like?id=' + fruit.id).then(resp => resp.json()).then(json => {
                            if (json.code === 10000) {
                                fruit.favs += 1
                            }
                        })
                    }
                }
            })
        </script>
        <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script>
        <script>
            // 通过ID选择器获取到绘图的div
            let chartDiv = document.querySelector('#chart')
            // 通过ECharts库的init方法将div初始化为绘图的画布
            let canvas = echarts.init(chartDiv)
            // 准备图表需要用到的数据选项
            let option = {
                title: {
                    text: '水果数据统计',
                    top: '2%',
                    left: '5%',
                    textStyle: {
                        color: '#4000ff',
                        fontFamily: 'FZJKJW--GB1-0',
                        fontSize: 24
                    }
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    top: '25%',
                    left: '2%'
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['40%', '80%'],
                        center: ['55%', '50%'],
                        data: []
                    }
                ]
            }

            function refreshData() {
                fetch('/api/favs').then(resp => resp.json()).then(json => {
                    option.series[0].data = json.data
                    // 通过setOption方法为画布设置数据选项
                    canvas.setOption(option)
                })
            }

            refreshData()
            window.setInterval(refreshData, 5000)
        </script>
    </body>
</html>